import type { UnInstance } from '@uni-helper/uni-network'
import { un } from '@uni-helper/uni-network'

// eslint-disable-next-line import/no-mutable-exports
let httpInstance: UnInstance = un.create({
  baseUrl: 'https://tongdadianzhao.tykhrs.site',
  timeout: 5000,
})

async function initHttpInstance(domain: string) {
  httpInstance = un.create({
    baseUrl: domain,
    timeout: 5000,
  })
}

httpInstance.interceptors.request.use(
  (config) => {
    return config
  },
  (error) => {
    uni.showToast({ title: '请求失败', icon: 'none' })
    return Promise.reject(error)
  },
)

httpInstance.interceptors.response.use(
  (response) => {
    return response
  },
  (error) => {
    uni.showToast({ title: JSON.stringify(error), icon: 'none' })
    return Promise.reject(error)
  },
)

export { httpInstance, initHttpInstance }
